home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 March / PCWorld_2007-03_cd.bin / v cisle / httrack / httrack-3.41-2.exe / {app} / libtest / callbacks-example-simple.c < prev    next >
C/C++ Source or Header  |  2006-06-05  |  3KB  |  86 lines

  1. /*
  2.     HTTrack external callbacks example : print all downloaded html documents
  3.  
  4.     How to build: (callback.so or callback.dll)
  5.       With GNU-GCC:
  6.         gcc -O -g3 -Wall -D_REENTRANT -shared -o mycallback.so callbacks-example.c -lhttrack1
  7.       With MS-Visual C++:
  8.         cl -LD -nologo -W3 -Zi -Zp4 -DWIN32 -Fe"mycallback.dll" callbacks-example.c libhttrack1.lib
  9.  
  10.       Note: the httrack library linker option is only necessary when using libhttrack's functions inside the callback
  11.  
  12.     How to use:
  13.       httrack --wrapper mycallback ..
  14. */
  15.  
  16. /* system includes */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20.  
  21. /* standard httrack module includes */
  22. #include "httrack-library.h"
  23. #include "htsopt.h"
  24. #include "htsdefines.h"
  25.  
  26. /* local function called as "check_html" callback */
  27. static int process_file(t_hts_callbackarg /*the carg structure, holding various information*/*carg, /*the option settings*/httrackp *opt, 
  28.                         /*other parameters are callback-specific*/
  29.                         char* html, int len, const char* url_address, const char* url_file) {
  30.   void *ourDummyArg = (void*) CALLBACKARG_USERDEF(carg);    /*optional user-defined arg*/
  31.  
  32.   /* call parent functions if multiple callbacks are chained. you can skip this part, if you don't want previous callbacks to be called. */
  33.   if (CALLBACKARG_PREV_FUN(carg, check_html) != NULL) {
  34.     if (!CALLBACKARG_PREV_FUN(carg, check_html)(CALLBACKARG_PREV_CARG(carg), opt,
  35.                                                 html, len, url_address, url_file)) {
  36.         return 0;  /* abort */
  37.       }
  38.   }
  39.  
  40.   printf("file %s%s content: %s\n", url_address, url_file, html);
  41.   return 1;  /* success */
  42. }
  43.  
  44. /* local function called as "end" callback */
  45. static int end_of_mirror(t_hts_callbackarg /*the carg structure, holding various information*/*carg, /*the option settings*/httrackp *opt) {
  46.   void *ourDummyArg = (void*) CALLBACKARG_USERDEF(carg);    /*optional user-defined arg*/
  47.  
  48.   /* processing */
  49.   fprintf(stderr, "That's all, folks!\n");
  50.  
  51.   /* call parent functions if multiple callbacks are chained. you can skip this part, if you don't want previous callbacks to be called. */
  52.   if (CALLBACKARG_PREV_FUN(carg, end) != NULL) {
  53.     /* status is ok on our side, return other callabck's status */
  54.     return CALLBACKARG_PREV_FUN(carg, end)(CALLBACKARG_PREV_CARG(carg), opt);
  55.   }
  56.  
  57.   return 1;  /* success */
  58. }
  59.  
  60. /*
  61. module entry point
  62. the function name and prototype MUST match this prototype
  63. */
  64. EXTERNAL_FUNCTION int hts_plug(httrackp *opt, const char* argv) {
  65.   /* optional argument passed in the commandline we won't be using here */
  66.   const char *arg = strchr(argv, ',');
  67.   if (arg != NULL)
  68.     arg++;
  69.  
  70.   /* plug callback functions */
  71.   CHAIN_FUNCTION(opt, check_html, process_file, /*optional user-defined arg*/NULL);
  72.   CHAIN_FUNCTION(opt, end, end_of_mirror, /*optional user-defined arg*/NULL);
  73.  
  74.   return 1;  /* success */
  75. }
  76.  
  77. /*
  78. module exit point
  79. the function name and prototype MUST match this prototype
  80. */
  81. EXTERNAL_FUNCTION int hts_unplug(httrackp *opt) {
  82.   fprintf(stderr, "Module unplugged");
  83.  
  84.   return 1;  /* success */
  85. }
  86.